home *** CD-ROM | disk | FTP | other *** search
/ Die Speccy' 97 / Die Speccy' 97.iso / amiga_system / the_aminet / comm / bbs / wwbbs31_source.lha / WWBBS / RxSrc / email.c < prev    next >
C/C++ Source or Header  |  1995-03-30  |  16KB  |  624 lines

  1. #include <exec/types.h>
  2. #include <exec/exec.h>
  3. #include <libraries/wwbbs.h>
  4. #include <ctype.h>
  5. #include <math.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9.  
  10. #include <proto/dos.h>
  11. #include <proto/exec.h>
  12. #include <proto/rexxsyslib.h>
  13. #include <proto/wwbbs.h>
  14.  
  15. #include "rx.h"
  16.  
  17. #include "email.h"
  18.  
  19. void EmailCommand(BYTE *id,UWORD cmd,BYTE *arg)
  20.     {
  21.         BOOL updatevars=TRUE;
  22.         switch(cmd)
  23.             {
  24.                 case EMAIL_Next:
  25.                     if(EmailNext(id))
  26.                         updatevars=FALSE;
  27.                     break;
  28.                 case EMAIL_Current:
  29.                     if(email_current)
  30.                         {
  31.                             APTR group;
  32.                             if(group=OpenMessageGroup(NULL,NULL,SHARED_LOCK))
  33.                                 {
  34.                                     EmailDisplay(id,group);
  35.                                     CloseMessageGroup(group);
  36.                                 }
  37.                             updatevars=FALSE;
  38.                         }
  39.                     else
  40.                         printf("~s\nCurrent email does not exist.\n");
  41.                     break;
  42.                 case EMAIL_Previous:
  43.                     if(EmailPrevious(id))
  44.                         updatevars=FALSE;
  45.                     break;
  46.                 case EMAIL_JumpTo:
  47.                     if(EmailJumpTo(id))
  48.                         updatevars=FALSE;
  49.                     break;
  50.                 case EMAIL_List:
  51.                     EmailList(id);
  52.                     updatevars=FALSE;
  53.                     break;
  54.                 case EMAIL_Write:
  55.                     EmailWrite(id);
  56.                     break;
  57.                 case EMAIL_ReplyTo:
  58.                     EmailReplyTo(id);
  59.                     break;
  60.                 case EMAIL_Delete:
  61.                     EmailDelete(id);
  62.                     break;
  63.                 case EMAIL_Feedback:
  64.                     EmailFeedback(id);
  65.                     break;
  66.                 case EMAIL_ShowNew:
  67.                     EmailShowNew(id);
  68.                     break;
  69.                 default:
  70.                     updatevars=FALSE;
  71.                     break;
  72.             }
  73.         if(updatevars)
  74.             EmailUpdateVars(id);
  75.     }
  76.  
  77. /***/
  78.  
  79. BOOL EmailNext(BYTE *id)
  80.     {
  81.         BOOL ret=FALSE;
  82.         APTR group;
  83.         if(group=OpenMessageGroup(NULL,NULL,SHARED_LOCK))
  84.             {
  85.                 ULONG num=0;
  86.                 num=email_current;
  87.                 while(num=GetNextMessage(group,num))
  88.                     {
  89.                         if(EmailIsAddressee(id,group,num))
  90.                             {
  91.                                 email_current=num;
  92.                                 break;
  93.                             }
  94.                     }
  95.                 if(num)
  96.                     {
  97.                         EmailDisplay(id,group);
  98.                         ret=TRUE;
  99.                     }
  100.                 else
  101.                     printf("~s\nNext email does not exist.\n");
  102.                 CloseMessageGroup(group);
  103.             }
  104.         return(ret);
  105.     }
  106.  
  107. BOOL EmailPrevious(BYTE *id)
  108.     {
  109.         BOOL ret=FALSE;
  110.         APTR group;
  111.         if(group=OpenMessageGroup(NULL,NULL,SHARED_LOCK))
  112.             {
  113.                 ULONG num=0;
  114.                 num=(email_current) ? email_current : (ULONG) ~0;
  115.                 while(num=GetPreviousMessage(group,num))
  116.                     {
  117.                         if(EmailIsAddressee(id,group,num))
  118.                             {
  119.                                 email_current=num;
  120.                                 break;
  121.                             }
  122.                     }
  123.                 if(num)
  124.                     {
  125.                         EmailDisplay(id,group);
  126.                         ret=TRUE;
  127.                     }
  128.                 else
  129.                     printf("~s\nPrevious email does not exist.\n");
  130.                 CloseMessageGroup(group);
  131.             }
  132.         return(ret);
  133.     }
  134.  
  135. BOOL EmailJumpTo(BYTE *id)
  136.     {
  137.         BOOL ret=FALSE;
  138.         APTR group;
  139.         if(group=OpenMessageGroup(NULL,NULL,SHARED_LOCK))
  140.             {
  141.                 ULONG count=0;
  142.                 {
  143.                     ULONG next=0;
  144.                     while(next=GetNextMessage(group,next))
  145.                         {
  146.                             if(EmailIsAddressee(id,group,next))
  147.                                 count++;
  148.                         }
  149.                 }
  150.                 if(count)
  151.                     {
  152.                         BYTE buff[11];
  153.                         strcpy(buff,"");
  154.                         printf("~p\nEnter number of email to jump to, 1-%ld.\n: ",count);
  155.                         if(GetLine(buff,10,GLFLG_Digits))
  156.                             {
  157.                                 if(atoi(buff)>=1 && atoi(buff)<=count)
  158.                                     {
  159.                                         ULONG num=0;
  160.                                         int i;
  161.                                         for(i=0;i<atoi(buff);i++)
  162.                                             {
  163.                                                 while(num=GetNextMessage(group,num))
  164.                                                     {
  165.                                                         if(EmailIsAddressee(id,group,num))
  166.                                                             break;
  167.                                                     }
  168.                                                 if(!num)
  169.                                                     break;
  170.                                             }
  171.                                         if(num)
  172.                                             {
  173.                                                 email_current=num;
  174.                                                 EmailDisplay(id,group);
  175.                                                 ret=TRUE;
  176.                                             }
  177.                                         else
  178.                                             printf("~s\nUnable to jump to email number %d.\n",atoi(buff));
  179.                                     }
  180.                                 else
  181.                                     printf("~s\nOut of range.\n");
  182.                             }
  183.                     }
  184.                 else
  185.                     printf("~s\nYou have no email.\n");
  186.                 CloseMessageGroup(group);
  187.             }
  188.         return(ret);
  189.     }
  190.  
  191. void EmailList(BYTE *id)
  192.     {
  193.         BYTE rows[4];
  194.         strcpy(rows,"");
  195.         if(GetVar("ROWS",rows,3,NULL)!=-1)
  196.             {
  197.                 APTR group;
  198.                 if(group=OpenMessageGroup(NULL,NULL,SHARED_LOCK))
  199.                     {
  200.                         ULONG count=0;
  201.                         {
  202.                             ULONG next=0;
  203.                             while(next=GetNextMessage(group,next))
  204.                                 {
  205.                                     if(EmailIsAddressee(id,group,next))
  206.                                         count++;
  207.                                 }
  208.                         }
  209.                         if(count)
  210.                             {
  211.                                 BYTE buff[21];
  212.                                 strcpy(buff,"");
  213.                                 printf("~p\nEnter range of email to list, 1-%ld, or `All' to list all email.\n: ",count);
  214.                                 if(GetLine(buff,20,NULL))
  215.                                     {
  216.                                         if(!stricmp(buff,"ALL"))
  217.                                             sprintf(buff,"1-%ld",count);
  218.                                         {
  219.                                             ULONG next=0;
  220.                                             BOOL first=FALSE;
  221.                                             int i=0,line=0;
  222.                                             int numwidth;
  223.                                             numwidth=(int) (log10((double) count)+1);
  224.                                             if(numwidth<3) numwidth=3;
  225.                                             while(next=GetNextMessage(group,next))
  226.                                                 {
  227.                                                     if(EmailIsAddressee(id,group,next))
  228.                                                         {
  229.                                                             i++;
  230.                                                             if(IsRange(buff,i))
  231.                                                                 {
  232.                                                                     if(WaitForChar(Input(),0))
  233.                                                                         {
  234.                                                                             getchar();
  235.                                                                             break;
  236.                                                                         }
  237.                                                                     if(!first)
  238.                                                                         {
  239.                                                                             printf("\n~h%*sNum From                 Date      Subject%*s\n~o",numwidth-3,"",37-(numwidth-3),"");
  240.                                                                             line++;
  241.                                                                             first=TRUE;
  242.                                                                         }
  243.                                                                     {
  244.                                                                         BYTE *from=NULL,*subject=NULL;
  245.                                                                         struct DateStamp *date=NULL;
  246.                                                                         char date_buff[16];
  247.                                                                         strcpy(date_buff,"");
  248.                                                                         if(GetMessageTags(group,MSGTAG_ID,next,MSGTAG_From,&from,MSGTAG_Subject,&subject,MSGTAG_Date,&date,TAG_END))
  249.                                                                             {
  250.                                                                                 {
  251.                                                                                     struct DateTime dt;
  252.                                                                                     dt.dat_Stamp=*date;
  253.                                                                                     dt.dat_Format=FORMAT_USA;
  254.                                                                                     dt.dat_Flags=DTF_SUBST;
  255.                                                                                     dt.dat_StrDay=NULL;
  256.                                                                                     dt.dat_StrDate=date_buff;
  257.                                                                                     dt.dat_StrTime=NULL;
  258.                                                                                     DateToStr(&dt);
  259.                                                                                 }
  260.                                                                                 printf("%*d %-20.20s %-9.9s %-*.*s\n",numwidth,i,from,date_buff,47-numwidth,47-numwidth,subject);
  261.                                                                                 line++;
  262.                                                                                 if(line==atoi(rows)-1)
  263.                                                                                     {
  264.                                                                                         printf("~pMore ([Y],N)? ");
  265.                                                                                         if(!Ask(NULL,TRUE))
  266.                                                                                             break;
  267.                                                                                         printf("~o");
  268.                                                                                         line=0;
  269.                                                                                     }
  270.                                                                             }
  271.                                                                     }
  272.                                                                 }
  273.                                                         }
  274.                                                 }
  275.                                             if(!first)
  276.                                                 printf("~s\nNo email found in specified range.\n");
  277.                                         }
  278.                                     }
  279.                             }
  280.                         else
  281.                             printf("~s\nYou have no email.\n");
  282.                         CloseMessageGroup(group);
  283.                     }
  284.             }
  285.     }
  286.  
  287. void EmailWrite(BYTE *id)
  288.     {
  289.         BYTE from[33],to[33];
  290.         strcpy(from,"");
  291.         strcpy(to,"");
  292.         GetStatusTags(STTAG_Name,id,STTAG_UserName,from,TAG_END);
  293.         if(strlen(from))
  294.             {
  295.                 printf("~p\nEnter the name of the user to whom you would like to send email.\n: ");
  296.                 if(GetLine(to,32,GLFLG_Format))
  297.                     {
  298.                         if(GetUserTags(USRTAG_Name,to,USRTAG_Exists,TRUE,TAG_END))
  299.                             {
  300.                                 BYTE subject[65];
  301.                                 strcpy(subject,"");
  302.                                 printf("~p\nEnter a subject for the email.\n: ");
  303.                                 if(GetLine(subject,64,NULL))
  304.                                     EmailSend(id,from,to,subject);
  305.                             }
  306.                         else
  307.                             printf("~s\nUser `%s' does not exist.\n",to);
  308.                     }
  309.             }
  310.     }
  311.  
  312. void EmailReplyTo(BYTE *id)
  313.     {
  314.         BYTE from[33],to[33],subject[65];
  315.         strcpy(from,"");
  316.         strcpy(to,"");
  317.         strcpy(subject,"");
  318.         GetStatusTags(STTAG_Name,id,STTAG_UserName,from,TAG_END);
  319.         if(strlen(from))
  320.             {
  321.                 if(email_current)
  322.                     {
  323.                         APTR group;
  324.                         if(group=OpenMessageGroup(NULL,NULL,SHARED_LOCK))
  325.                             {
  326.                                 BYTE *from_buff=NULL,*subject_buff=NULL;
  327.                                 if(GetMessageTags(group,MSGTAG_ID,email_current,MSGTAG_From,&from_buff,MSGTAG_Subject,&subject_buff,TAG_END))
  328.                                     {
  329.                                         if(strlen(from_buff)<=32)
  330.                                             strcpy(to,from_buff);
  331.                                         if(!strnicmp("Re:",subject_buff,3))
  332.                                             strcpy(subject,subject_buff);
  333.                                         else
  334.                                             sprintf(subject,"Re: %.60s",subject_buff);
  335.                                     }
  336.                                 CloseMessageGroup(group);
  337.                             }
  338.                         if(strlen(to))
  339.                             EmailSend(id,from,to,subject);
  340.                         else
  341.                             printf("~s\nUnable to send email.\n");
  342.                     }
  343.                 else
  344.                     printf("~s\nCurrent email does not exist.\n");
  345.             }
  346.     }
  347.  
  348. void EmailDelete(BYTE *id)
  349.     {
  350.         if(email_current)
  351.             {
  352.                 APTR group;
  353.                 if(group=OpenMessageGroup(NULL,NULL,EXCLUSIVE_LOCK))
  354.                     {
  355.                         if(RemMessageTags(group,MSGTAG_ID,email_current))
  356.                             printf("~s\nEmail deleted.\n");
  357.                         else
  358.                             printf("~s\nUnable to delete email.\n");
  359.                         CloseMessageGroup(group);
  360.                     }
  361.                 else
  362.                     printf("~s\nUnable to delete email.\n");
  363.             }
  364.         else
  365.             printf("~s\nCurrent email does not exist.\n");
  366.     }
  367.  
  368. void EmailFeedback(BYTE *id)
  369.     {
  370.         BYTE from[33];
  371.         strcpy(from,"");
  372.         GetStatusTags(STTAG_Name,id,STTAG_UserName,from,TAG_END);
  373.         if(strlen(from))
  374.             {
  375.                 BYTE subject[65];
  376.                 strcpy(subject,"");
  377.                 printf("~p\nEnter a subject for the feedback.\n: ");
  378.                 if(GetLine(subject,64,NULL))
  379.                     EmailSend(id,from,"Sysop",subject);
  380.             }
  381.     }
  382.  
  383. void EmailShowNew(BYTE *id)
  384.     {
  385.         APTR group;
  386.         if(group=OpenMessageGroup(NULL,NULL,SHARED_LOCK))
  387.             {
  388.                 ULONG count=0;
  389.                 {
  390.                     ULONG next=0;
  391.                     while(next=GetNextMessage(group,next))
  392.                         {
  393.                             if(EmailIsAddressee(id,group,next))
  394.                                 count++;
  395.                         }
  396.                 }
  397.                 if(count)
  398.                     printf("~s\nYou have %ld pieces of email waiting.\n",count);
  399.                 else
  400.                     printf("~s\nYou have no mail.\n");
  401.                 CloseMessageGroup(group);
  402.             }
  403.     }
  404.  
  405. /***/
  406.  
  407. BOOL EmailIsAddressee(BYTE *id,APTR group,ULONG num)
  408.     {
  409.         BOOL ret=FALSE;
  410.         BOOL onlyrealnames=FALSE;
  411.         BYTE name[33],username[33],realname[33],uucpname[9];
  412.         strcpy(name,"");
  413.         strcpy(username,"");
  414.         strcpy(realname,"");
  415.         strcpy(uucpname,"");
  416.         GetConfigTags(CFGTAG_Name,"System",SYSTAG_OnlyRealNames,&onlyrealnames,TAG_END);
  417.         GetStatusTags(STTAG_Name,id,STTAG_UserName,name,TAG_END);
  418.         if(strlen(name))
  419.             {
  420.                 BYTE *to=NULL;
  421.                 GetUserTags(USRTAG_Name,name,USRTAG_UserName,username,USRTAG_RealName,realname,USRTAG_UUCPName,uucpname,TAG_END);
  422.                 if(GetMessageTags(group,MSGTAG_ID,num,MSGTAG_To,&to,TAG_END))
  423.                     {
  424.                         if(!stricmp(to,"Sysop"))
  425.                             {
  426.                                 BYTE sysop_username[33],sysop_realname[33],sysop_uucpname[9];
  427.                                 strcpy(sysop_username,"");
  428.                                 strcpy(sysop_realname,"");
  429.                                 strcpy(sysop_uucpname,"");
  430.                                 GetUserTags(USRTAG_Name,"Sysop",USRTAG_UserName,sysop_username,USRTAG_RealName,sysop_realname,USRTAG_UUCPName,sysop_uucpname,TAG_END);
  431.                                 if((!onlyrealnames && !stricmp(sysop_username,username)) || !stricmp(sysop_realname,realname) || !stricmp(sysop_uucpname,uucpname))
  432.                                     ret=TRUE;
  433.                             }
  434.                         else
  435.                             {
  436.                                 if((!onlyrealnames && !stricmp(to,username)) || !stricmp(to,realname) || !stricmp(to,uucpname))
  437.                                     ret=TRUE;
  438.                             }
  439.                     }
  440.             }
  441.         return(ret);
  442.     }
  443.  
  444. void EmailDisplay(BYTE *id,APTR group)
  445.     {
  446.         BYTE *from=NULL,*to=NULL,*subject=NULL,*text=NULL;
  447.         struct DateStamp *date=NULL;
  448.         if(GetMessageTags(group,MSGTAG_ID,email_current,
  449.                 MSGTAG_From,&from,
  450.                 MSGTAG_To,&to,
  451.                 MSGTAG_Subject,&subject,
  452.                 MSGTAG_Text,&text,
  453.                 MSGTAG_Date,&date,
  454.                 TAG_END))
  455.             {
  456.                 printf("\n~h");
  457.                 {
  458.                     char buff[64];
  459.                     EmailUpdateVars(id);
  460.                     sprintf(buff,"[%ld/%ld]",email_promptcurrent,email_prompthigh);
  461.                     printf("[From   ] %-32s %36.36s\n",from,buff);
  462.                 }
  463.                 if(to && strlen(to))
  464.                     printf("[To     ] %-69s\n",to);
  465.                 {
  466.                     char date_buff[32];
  467.                     strcpy(date_buff,"");
  468.                     {
  469.                         struct DateTime dt;
  470.                         char time_buff[16];
  471.                         strcpy(time_buff,"");
  472.                         dt.dat_Stamp=*date;
  473.                         dt.dat_Format=FORMAT_USA;
  474.                         dt.dat_Flags=DTF_SUBST;
  475.                         dt.dat_StrDay=NULL;
  476.                         dt.dat_StrDate=date_buff;
  477.                         dt.dat_StrTime=time_buff;
  478.                         DateToStr(&dt);
  479.                         strcat(date_buff," ");
  480.                         strcat(date_buff,time_buff);
  481.                     }
  482.                     printf("[Date   ] %-69s\n",date_buff);
  483.                 }
  484.                 printf("[Subject] %-69.69s\n",subject);
  485.                 if(text)
  486.                     {
  487.                         printf("\n");
  488.                         Pager(text,(to && strlen(to)) ? 5 : 4);
  489.                     }
  490.                 {
  491.                     BYTE user[33];
  492.                     strcpy(user,"");
  493.                     GetStatusTags(STTAG_Name,id,STTAG_UserName,user,TAG_END);
  494.                     if(strlen(user))
  495.                         {
  496.                             ULONG emailread=0;
  497.                             GetUserTags(USRTAG_Name,user,USRTAG_EmailRead,&emailread,TAG_END);
  498.                             emailread++;
  499.                             SetUserTags(USRTAG_Name,user,USRTAG_EmailRead,emailread,TAG_END);
  500.                         }
  501.                 }
  502.             }
  503.         else
  504.             printf("~s\nCurrent email does not exist.\n");
  505.     }
  506.  
  507. void EmailSend(BYTE *id,BYTE *from,BYTE *to,BYTE *subject)
  508.     {
  509.         BYTE filename[64];
  510.         sprintf(filename,"T:wwbbs_email.%s",id);
  511.         if(RunEditor(filename))
  512.             {
  513.                 BYTE *text=NULL;
  514.                 {
  515.                     BPTR fh_signature,fh_msg;
  516.                     BYTE signature_file[256];
  517.                     strcpy(signature_file,"");
  518.                     if(GetVar("HOME",signature_file,255,NULL)!=-1)
  519.                         {
  520.                             AddPart(signature_file,".signature",255);
  521.                             if(fh_signature=Open(signature_file,MODE_OLDFILE))
  522.                                 {
  523.                                     if(fh_msg=Open(filename,MODE_READWRITE))
  524.                                         {
  525.                                             Seek(fh_msg,0,OFFSET_END);
  526.                                             {
  527.                                                 BYTE buff[256];
  528.                                                 while(FGets(fh_signature,buff,255))
  529.                                                     FPuts(fh_msg,buff);
  530.                                             }
  531.                                             Close(fh_msg);
  532.                                         }
  533.                                     Close(fh_signature);
  534.                                 }
  535.                         }
  536.                 }
  537.                 {
  538.                     BPTR fh;
  539.                     if(fh=Open(filename,MODE_OLDFILE))
  540.                         {
  541.                             __aligned struct FileInfoBlock fib;
  542.                             if(ExamineFH(fh,&fib))
  543.                                 {
  544.                                     if(text=AllocVec(fib.fib_Size+1,MEMF_CLEAR))
  545.                                         {
  546.                                             if(FRead(fh,text,sizeof(BYTE),fib.fib_Size)==fib.fib_Size)
  547.                                                 {
  548.                                                     APTR group;
  549.                                                     if(group=OpenMessageGroup(NULL,NULL,EXCLUSIVE_LOCK))
  550.                                                         {
  551.                                                             ULONG num;
  552.                                                             if(num=AddMessageTags(group,MSGTAG_DontSave,TRUE,TAG_END))
  553.                                                                 {
  554.                                                                     if(SetMessageTags(group,MSGTAG_ID,num,MSGTAG_From,from,MSGTAG_To,to,MSGTAG_Subject,subject,MSGTAG_Text,text,TAG_END))
  555.                                                                         {
  556.                                                                             {
  557.                                                                                 BYTE user[33];
  558.                                                                                 strcpy(user,"");
  559.                                                                                 GetStatusTags(STTAG_Name,id,STTAG_UserName,user,TAG_END);
  560.                                                                                 if(strlen(user))
  561.                                                                                     {
  562.                                                                                         ULONG emailwritten=0;
  563.                                                                                         GetUserTags(USRTAG_Name,user,USRTAG_EmailWritten,&emailwritten,TAG_END);
  564.                                                                                         emailwritten++;
  565.                                                                                         SetUserTags(USRTAG_Name,user,USRTAG_EmailWritten,emailwritten,TAG_END);
  566.                                                                                     }
  567.                                                                             }
  568.                                                                             printf("~s\nEmail sent.\n");
  569.                                                                         }
  570.                                                                     else
  571.                                                                         {
  572.                                                                             RemMessageTags(group,MSGTAG_ID,num,TAG_END);
  573.                                                                             printf("~s\nUnable to send email.\n");
  574.                                                                         }
  575.                                                                 }
  576.                                                             else
  577.                                                                 printf("~s\nUnable to send email.\n");
  578.                                                             CloseMessageGroup(group);
  579.                                                         }
  580.                                                 }
  581.                                             else
  582.                                                 printf("~s\nUnable to send email.\n");
  583.                                             FreeVec(text);
  584.                                         }
  585.                                     else
  586.                                         printf("~s\nUnable to send email.\n");
  587.                                 }
  588.                             else
  589.                                 printf("~s\nUnable to send email.\n");
  590.                             Close(fh);
  591.                         }
  592.                     else
  593.                         printf("~s\nUnable to send email.\n");
  594.                 }
  595.                 DeleteFile(filename);
  596.             }
  597.     }
  598.  
  599. void EmailUpdateVars(BYTE *id)
  600.     {
  601.         APTR group;
  602.         if(group=OpenMessageGroup(NULL,NULL,SHARED_LOCK))
  603.             {
  604.                 ULONG count=0;
  605.                 email_promptcurrent=0;
  606.                 {
  607.                     ULONG next=0;
  608.                     while(next=GetNextMessage(group,next))
  609.                         {
  610.                             if(EmailIsAddressee(id,group,next))
  611.                                 {
  612.                                     count++;
  613.                                     if(next==email_current)
  614.                                         email_promptcurrent=count;
  615.                                 }
  616.                         }
  617.                 }
  618.                 if(!email_promptcurrent)
  619.                     email_current=0;
  620.                 email_prompthigh=count;
  621.                 CloseMessageGroup(group);
  622.             }
  623.     }
  624.